package com.zx.feign.client.fallbackfactory;

import com.zx.core.wrapper.ObjectWrapper;
import com.zx.core.wrapper.ResponseWrapper;
import com.zx.feign.client.NotificationServiceClient;
import com.zx.feign.client.dto.GetNotificationTemplateResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

/**
 * <p>
 * description: 通知服务Feign客户端降级处理 <br>
 * create: 2025-10-15
 * </p>
 *
 * @author zhou  xun
 */
@Slf4j
@Component
public class NotificationServiceClientFallbackFactory implements FallbackFactory<NotificationServiceClient> {

    @Override
    public NotificationServiceClient create(Throwable throwable) {
        return new NotificationServiceClient() {
            @Override
            public ResponseWrapper sendNotification(String paramJsonStr) {
                log.error("通知服务调用失败，触发降级处理", throwable);
                ResponseWrapper response = new ResponseWrapper();
                response.fail("通知服务暂时不可用");
                return response;
            }

            /**
             * 重新发送系统通知
             *
             * @param notificationId 通知ID
             * @return {@link ResponseWrapper}
             */
            @Override
            public ResponseWrapper resendNotification(String notificationId) {
                log.error("通知服务调用失败，触发降级处理", throwable);
                ResponseWrapper response = new ResponseWrapper();
                response.fail("通知服务暂时不可用");
                return response;
            }

            /**
             * 根据模板编码获取通知模板
             *
             * @param templateCode 模板编码
             * @return 通知模板信息
             */
            @Override
            public ObjectWrapper<GetNotificationTemplateResponseDTO> getTemplateByCode(String templateCode) {
                log.error("通知服务调用失败，触发降级处理", throwable);
                ObjectWrapper<GetNotificationTemplateResponseDTO> response = new ObjectWrapper<>();
                response.fail("通知服务暂时不可用");
                return response;
            }
        };
    }
}